package com.dc.modules.sys.user.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.dc.commons.base.controller.DcBaseController;
import com.dc.commons.persistence.embeddable.Attachment;
import com.dc.commons.query.DcQuery;
import com.dc.commons.shiro.annotation.DcPermissions;
import com.dc.commons.shiro.annotation.DcPermissions.Logical;
import com.dc.commons.shiro.enums.PermissionType;
import com.dc.commons.sys.persistence.IAttachment;
import com.dc.commons.utils.ExcelExportor;
import com.dc.commons.views.ModelAndViewBuilder;
import com.dc.modules.sys.user.domain.DcSysUser;
import com.dc.modules.sys.user.service.DcSysUserService;

/*******************************************************************************<P>
 * 文件名: DcSysUserController.java <br>
 * 版本: 1.0<br>
 * 描述:<br>
 * 版权所有: <br>
 * <P>
 * 创建者: 王建栋 <br>
 * 创建日期: 2016年4月7日 下午2:43:37<br>
 * 修改者: <br>
 * 修改日期: <br>
 * 修改说明: <br>
 ******************************************************************************/
@Controller
@RequestMapping("/sys/user")
public class DcSysUserController extends DcBaseController{
	
	/**
	 * 用户管理
	 * @param dcSysUser
	 * @return ModelAndView
	 */
	@RequestMapping("/sysUserManager")
	@DcPermissions(name="用户管理",taskCode="sys:user:sysUserManager",permissionType=PermissionType.MENU)
	public ModelAndView sysUserManager(DcSysUser dcSysUser){
		dcSysUserService.sysUserManager(dcSysUser);
		
		return ModelAndViewBuilder.create()
				.success()
				.viewName("/sys/user/sysUserManager")
				.result(dcSysUser)
				.build();
	}
	
	/**
	 *  用户查询
	 * @param dcQuery
	 * @return ModelAndView
	 */
	@RequestMapping("/querySysUser")
	@DcPermissions(name="用户查询",taskCode="sys:user:sysUserManager",repeat=true)
	public ModelAndView querySysUser(DcQuery dcQuery){
		Page<DcSysUser> page=dcSysUserService.queryPage(dcQuery);
		
		return ModelAndViewBuilder.create()
				.success()
				.result(page)
				.build();
	}
	
	/**
	 * 到用户新增页面
	 * @param sysUser
	 * @return ModelAndView
	 */
	@RequestMapping("/sysUserAdd")
	@DcPermissions(name="新增用户",taskCode="sys:user:sysUserAdd",permissionType=PermissionType.TASK)
	public ModelAndView sysUserAdd(DcSysUser sysUser){
		sysUser.setUserName(sysUser.getUserName()+"admin");
		return ModelAndViewBuilder.create()
				.success()
				.viewName("/sys/user/sysUserAdd")
				.build();
	}
	
	
	/**
	 * 新增用户
	 * @param sysUser
	 * @return ModelAndView
	 */
	@RequestMapping("/addSysUser")
	@DcPermissions(name="新增用户",taskCode="sys:user:sysUserAdd",repeat=true)
	public ModelAndView addSysUser(DcSysUser dcSysUser){
		
		dcSysUser=dcSysUserService.addSysUser(dcSysUser);
		return ModelAndViewBuilder.create()
				.success()
				.result(dcSysUser)
				.build();
	}
	
	
	/**
	 * 到用户修改页面
	 * @param sysUser
	 * @return ModelAndView
	 */
	@RequestMapping("/sysUserEdit")
	@DcPermissions(name="修改用户",taskCode="sys:user:sysUserEdit",permissionType=PermissionType.TASK)
	public ModelAndView sysUserEdit(DcSysUser dcSysUser){
		
		return ModelAndViewBuilder.create()
				.success()
				.viewName("/sys/user/sysUserEdit")
				.build();
	}
	
	
	/**
	 * 修改用户
	 * @param sysUser
	 * @return ModelAndView
	 */
	@RequestMapping("/editSysUser")
	@DcPermissions(name="修改用户",taskCode="sys:user:sysUserEdit",repeat=true)
	public ModelAndView editSysUser(DcSysUser dcSysUser){
		dcSysUser=dcSysUserService.editSysUser(dcSysUser);
		return ModelAndViewBuilder.create()
				.success()
				.build();
	}
	
	/**
	 * 用户详情
	 * @param sysUser
	 * @return ModelAndView
	 */
	@RequestMapping("/sysUserDetail")
	@DcPermissions(name="用户详情",taskCode="sys:user:sysUserDetail",permissionType=PermissionType.TASK)
	public ModelAndView sysUserDetail(DcSysUser dcSysUser){
		
		return ModelAndViewBuilder.create()
				.success()
				.viewName("/sys/user/sysUserDetail")
				.build();
	}
	
	
	/**
	 *  用户导出
	 * @param request
	 * @param dcQuery
	 * @return ModelAndView
	 */
	@RequestMapping("/exportSysUser")
	@DcPermissions(name="导出用户",taskCode={"sys:user:sysUserExport","sys:user:sysUserImport"},logical=Logical.OR,permissionType=PermissionType.TASK)
	public ModelAndView exportSysUser(NativeWebRequest request,DcQuery dcQuery){
		 IAttachment attachment=ExcelExportor.export(dcSysUserService, dcQuery);
		
		return ModelAndViewBuilder.create()
				.success()
				.result(attachment)
				.include(Attachment.class, "name","type","link")
				.build();
	}
	
	
	/**
	 *  用户导入
	 * @param dcQuery
	 * @param attachment
	 * @return ModelAndView
	 */
	@RequestMapping("/importSysUser")
	@DcPermissions(name="导入用户",taskCode="sys:user:sysUserImport",permissionType=PermissionType.TASK)
	public ModelAndView importSysUser(DcQuery dcQuery,Attachment attachment){
		DcSysUser dcSysUser=new DcSysUser();
		dcSysUserService.importData(dcSysUser,attachment);
		
		return ModelAndViewBuilder.create()
				.success()
				.result(attachment)
				.build();
	}
	
	/**
	 *  启用 用户
	 * @param dcSysUser
	 * @return ModelAndView
	 */
	@RequestMapping("/enabledSysUser")
	@DcPermissions(name="启用用户",taskCode="sys:user:sysUserEnabled",permissionType=PermissionType.TASK)
	public ModelAndView enabledSysUser(DcSysUser dcSysUser){
		dcSysUserService.enabledSysUser(dcSysUser);
		
		return ModelAndViewBuilder.create()
				.success()
				.build();
	}
	
	
	/**
	 *  暂停 用户
	 * @param dcSysUser
	 * @return ModelAndView
	 */
	@RequestMapping("/suspendSysUser")
	@DcPermissions(name="暂停用户",taskCode="sys:user:sysUserSuspend",permissionType=PermissionType.TASK)
	public ModelAndView suspendSysUser(DcSysUser dcSysUser){
		dcSysUserService.suspendSysUser(dcSysUser);
		
		return ModelAndViewBuilder.create()
				.success()
				.build();
	}
	
	
	/**
	 *  停用 用户
	 * @param dcSysUser
	 * @return ModelAndView
	 */
	@RequestMapping("/deActivateSysUser")
	@DcPermissions(name="停用用户",taskCode="sys:user:sysUserDeActivate",permissionType=PermissionType.TASK)
	public ModelAndView deActivateSysUser(DcSysUser dcSysUser){
		dcSysUserService.deActivateSysUser(dcSysUser);
		
		return ModelAndViewBuilder.create()
				.success()
				.build();
	}
	
	
	/**
	 * 删除用户
	 * @param sysUser
	 * @return ModelAndView
	 */
	@RequestMapping("/deleteSysUser")
	@DcPermissions(name="删除用户",taskCode="sys:user:sysUserDelete",permissionType=PermissionType.TASK)
	public ModelAndView deleteSysUser(DcSysUser dcSysUser){
		dcSysUser=dcSysUserService.deleteSysUser(dcSysUser);
		
		return ModelAndViewBuilder.create()
				.success()
				.result(dcSysUser)
				.build();
	}
	
	/**
	 * 批量删除用户
	 * @param ids
	 * @return ModelAndView
	 */
	@RequestMapping("/deleteAllSysUser")
	@DcPermissions(name="批量删除用户",taskCode="sys:user:sysUserDeleteAll",permissionType=PermissionType.TASK)
	public ModelAndView deleteAllSysUser(String[] ids){
		dcSysUserService.deleteAllSysUser(ids);
		
		return ModelAndViewBuilder.create()
				.success()
				.build();
	}
	
	
	/**
	 *  如果参数中存在 id 则根据id查询到对象。
	 * @param id
	 * @param model
	 */
	@ModelAttribute
	public void get(@RequestParam(required=false,name="id") String id,Model model) {
		DcSysUser sysUser=new DcSysUser();
		if (StringUtils.isNotEmpty(id)){
			sysUser=dcSysUserService.findSysUser(id);
			model.addAttribute("dcSysUser", sysUser);
		}
	}
	
	@Autowired
	private DcSysUserService dcSysUserService;
}
